﻿using System;
using System.Linq;
using HeritageAcademy.Models.DataModels;
using HeritageAcademy.Models.Loaders;

namespace HeritageAcademy.Models.ViewModels
{
    /// <summary>
    /// Settings data handler.
    /// </summary>
    public class SettingsConfigViewModel
    {
        #region Parameters
        
        private readonly SettingsConfigLoader _settingsConfigLoader;
        private string _currentUserId;
        private Setting _currentSetting;
        
        #endregion

        #region Ctor
        
        public SettingsConfigViewModel()
        {
            _settingsConfigLoader = new SettingsConfigLoader(new Settings());
        }
        
        #endregion
      
        #region Properties
        
        public Setting Current
        {
            get { return _currentSetting = GetSettingFromId(_currentUserId); }
        }

        public bool LastSelectedViewIsLibrary
        {
            get { return Convert.ToBoolean(_currentSetting.LastSelectedViewIsLibrary); }
        }
        
        #endregion

        #region Methods
        
        public void InitializeSettings(string currentUserId)
        {
            // Select settings for current user
            _currentUserId = currentUserId;
            _currentSetting = GetSettingFromId(currentUserId);
        }
        
        /// <summary>
        /// Get setting object using its id. Setting id is equal to user's id.
        /// </summary>
        /// <param name="id">Setting object id</param>
        /// <returns>Setting object</returns>
        public Setting GetSettingFromId(string id)
        {
            object setting = null;
            foreach (var t in _settingsConfigLoader.Model.Setting.Where(t => t.Id == id))
                setting = t;
            return (Setting)setting;
        }
        
        #endregion
    }
}
